package appx.craft.asd.uc;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import appx.craft.asd.R;
import appx.craft.asd.bean.CountryBean;
import appx.craft.asd.utils.Const;
import appx.craft.asd.utils.Utility;

public class Country_Dialog extends Dialog implements OnClickListener,OnItemClickListener{
	

	/** The tag. */
	final String TAG = Country_Dialog.class.getCanonicalName();
	
	/** The m context. */
	Context mContext;
	
	/** The m activity. */
	Activity mActivity;
	
	/** The m country adapter. */
	Adapter_Country_listview mCountryAdapter;
	
	/** The listview_itemlist. */
	ListView listview_itemlist;
	
	/** The m country list. */
	ArrayList<CountryBean> mCountryList = new ArrayList<CountryBean>();
	
	/** The btn_red_close. */
	ImageView btn_red_close;
	
	/** The m callback. */
	OnHeadNavigationSelectedListener mCallback;
	
	/** The edittext_search. */
	EditText edittext_search;
	
	/** The textview_listview_empty. */
	TextView textview_listview_empty;
	
	public Country_Dialog(Context context) {
		super(context,R.style.CustomDialog);
		mContext = context;
		mActivity = (Activity)context;
		mCallback = (OnHeadNavigationSelectedListener)mContext;
		populateList();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.popup_country_winodw);
	
		
		btn_red_close = (ImageView)findViewById(R.id.btn_red_close);
		btn_red_close.setOnClickListener(this);
		
		listview_itemlist = (ListView)findViewById(R.id.listview_itemlist);
		listview_itemlist.setOnItemClickListener(this);
		mCountryAdapter = new Adapter_Country_listview(mContext,this);
		
		textview_listview_empty = (TextView)findViewById(R.id.textview_listview_empty);
		
		listview_itemlist.setAdapter(mCountryAdapter);
		
		mCountryAdapter.addItems(mCountryList);
		
		edittext_search = (EditText)findViewById(R.id.edittext_search);
		edittext_search.setText("");
		edittext_search.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				mCountryAdapter.getFilter().filter(s.toString());	
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
				
			}
		});
		
	
	}
	
	@Override
	public void onClick(View v) {
		edittext_search.setText(null);
		if(v.getId() == R.id.btn_red_close){
			if(isShowing()){
				hideSoftKeyBoard();
				dismiss();
			}
		}
	}
	
	public void closeDialog(){
		hideSoftKeyBoard();
		if(isShowing()){
			dismiss();
		}
	}
	
	public void showDialog(){
		
		if(!isShowing()){
			show();
			if (mCountryAdapter != null) {
				mCountryAdapter.addItems(mCountryList);	
				
			}
			
		}
	}
	
	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		edittext_search.setText("");
		CountryBean mObjBean =(CountryBean) listview_itemlist.getAdapter().getItem(position);
		mCallback.onButtonSelected(mObjBean);
	}
	
	/**
	 * Populate list.
	 */
	public void populateList(){
		
		new RetriveCountryCode().execute();
	}
	
	/**
	 * The Class RetriveCountryCode.
	 */
	class RetriveCountryCode extends AsyncTask<Void,Void,Integer>{

		/* (non-Javadoc)
		 * @see android.os.AsyncTask#doInBackground(Params[])
		 */
		@Override
		protected Integer doInBackground(Void... params) {
			ArrayList<CountryBean> readArrayList = Utility.readObject(Const.FILE_CODE_LIST);
			for (int k = 0; k < readArrayList.size() ; k++) {
				mCountryList.add(readArrayList.get(k));
			}
			return null;
		}
		
	}
	
	/**
	 * Hide soft key board.
	 */
	public void hideSoftKeyBoard(){
		InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(edittext_search.getWindowToken(),0);
	}
	
	/**
	 * Show empty box.
	 *
	 * @param showhide the showhide
	 */
	public void showEmptyBox(boolean showhide){
		if(showhide){
			
			textview_listview_empty.post(new Runnable() {
				
				@Override
				public void run() {
					textview_listview_empty.setVisibility(View.VISIBLE);	
				}
			});
		}else{
			
			textview_listview_empty.post(new Runnable() {
				
				@Override
				public void run() {
					textview_listview_empty.setVisibility(View.INVISIBLE);	
				}
			});
			
		}
	}
	
	
	
	
}
